/*
 *
 */

jsx3.Class.defineClass(
        "tibco.admin.enterpriseAssets.shdResDefs.Identity.ShdIdentityResDefs", //the full name of the class to create
        jsx3.lang.Object, //  extends class
        [tibco.admin.enterpriseAssets.shdResDefs.ShdResDefsInterface], // implements interface
        function (plugin) {

            plugin.prototype.details = null;
            // TODO: handle multiple instances?

            // Implementation of the ShdResDefsInterface
            plugin.instance = null

            plugin.prototype.initPlugin = function() {
                tibco.admin.Main.loadResource("tibco/admin/enterpriseAssets/shdResDefs/Identity/jss/jss.xml", "tibco.admin", "jss_25K", "ljss", false);
            }

            plugin.prototype.destroyPlugin = function() {
            }

            plugin.getInstance = function(args) {
                if(!this.instance) {
                    this.instance = new tibco.admin.enterpriseAssets.shdResDefs.Identity.ShdIdentityResDefs();
                }
                return this.instance;
            }

            plugin.prototype.loadComponent = function(parent, master, id, name, description) {
                var me = this;
                this.details = new tibco.admin.enterpriseAssets.shdResDefs.Identity.ShdIdentityResDefDetails(master, parent, id,
                        name, description, me);
                this.details.loadView();
                return this.details;
            }

            plugin.prototype.getType = function() {
                return "IDENTITY";
            }

            plugin.prototype.getAddText = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@AddText_Identity");
            }

            plugin.prototype.getImagePath = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@addin@Identity Icon Path");
            }

            plugin.prototype.addSharedResourceDefinition = function(parent, master) {
                var me = this;
                this.details = new tibco.admin.enterpriseAssets.shdResDefs.Identity.ShdIdentityResDefDetails(master, parent, null,
                        null, null, me);
                this.details.loadAdd();
                return this.details;
            }
            // End implementation of the ShdResDefsInterface

        });

jsx3.Class.defineClass(
        "tibco.admin.enterpriseAssets.shdResDefs.Identity.ShdIdentityResDefDetails", //the full name of the class
        tibco.admin.enterpriseAssets.shdResDefs.ShdResDefDetails, //  extends ShdResDefDetails
        null, // implements no interface
        function (details) {
            details.ONTYPESELECTED = "tibco.admin.enterpriseAssets.shdResDefs.Identity.TYPESELECTED";
            details.prototype.helpID = "EnterpriseAssets.SharedResourceDefinitions.SharedResourceDefinitions.Identity";

            details.prototype.init = function(master, parent, id, name, description, handler) {
                this.jsxsuper(master, parent, id, name, description, handler);
                this.editableComponentList.addAll(["cmbURL_25K", "txtAlias_25K", "txtAliasPassword_25K"], this.editableComponentList.size());
                this.rwComponentList.addAll(["cmbURL_25K", "txtAlias_25K", "txtAliasPassword_25K"], this.rwComponentList.size());
                this.roComponentList.addAll(["lblcURL_25K", "lblcAlias_25K", "lblcAliasPassword_25K"], this.roComponentList.size());
            }

            details.prototype.getType = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@SRDType_Identity");
            }

            details.prototype.getHelpId = function() {
                return this.helpID;
            }

            details.prototype.getDetailsOperation = function() {
                return "getIdentityResourceDetails";
            }
            details.prototype.getDetailsOperationRules = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/Identity/rules/getIdentityResourceDetails.xml";
            }
            details.prototype.editOperation = function() {
                return "editIdentityResource";
            }
            details.prototype.editOperationRules = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/Identity/rules/editIdentityResource.xml";
            }
            details.prototype.addOperation = function() {
                return "addIdentityResource";
            }
            details.prototype.addOperationRules = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/Identity/rules/addIdentityResource.xml";
            }
            details.prototype.getDetailsComponent = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/Identity/components/shdIdentityResDefDetails.xml";
            }
            details.prototype.getSecondaryDetailsComponent = function() {
                return null;
            }

            // TODO: make name unique
            details.prototype.getDefaultName = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@defaultName_Identity");
            }
            details.prototype.getDefaultDescription = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@defaultDescription_Identity");
            }

            details.prototype.initializeGenericDetails = function(objEvent) {
                this.jsxsuper(objEvent);
            }

            details.prototype.initializeSpecificDetails = function(objEvent) {
                if(objEvent.status == "SUCCESS") {
                    this.callgetKeystoresRegisteredWithEnterprise();
                }
                this.jsxsuper(objEvent);

                objEvent.obj.handler = this;
            }

            details.prototype.initializeGenericAdd = function(objEvent) {
                this.jsxsuper(objEvent);
            }

            details.prototype.initializeSpecificAdd = function(objEvent) {
                if(objEvent.status == "SUCCESS") {
                    this.callgetKeystoresRegisteredWithEnterprise();
                }
                this.jsxsuper(objEvent);

                objEvent.obj.handler = this;
                this.initializeIdentityParam();
            }

            details.prototype.loadEdit = function() {
                this.jsxsuper();
                this.initializeIdentityParam();
            }

            details.prototype.ongetConfigDetailsSuccess = function(objEvent) {
                this.callgetKeystoresRegisteredWithEnterprise();
                var response = tibco.admin.Main.getServer().Cache.getDocument("xmlCacheIDGetIdentityDetailsResponse_25K").getRecordNode("jsxroot");
                this.jsxsuper(response);

                this.mainComponent.getDescendantOfName("lblcSubtype_25K").setText(tibco.admin.Main.getServer().getDynamicProperty("@@PublicKeyIdentity_25K"), true);
                var value = response.getAttribute("userActionForKeyStoreSubType");
                this.mainComponent.getDescendantOfName("userActionForKeyStoreSubType_25K").setValue(value, true);

                value = response.getAttribute("keyStoreName");
                this.mainComponent.getDescendantOfName("lblcURL_25K").setText(value, true);

                value = response.getAttribute("keyStoreEntityID");
                this.mainComponent.getDescendantOfName("cmbURL_25K").setValue(value, true);
                this.mainComponent.getDescendantOfName("blkURL_25K").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                value = response.getAttribute("alias");
                this.mainComponent.getDescendantOfName("lblcAlias_25K").setText(value, true);
                this.mainComponent.getDescendantOfName("txtAlias_25K").setValue(value, true);
                this.mainComponent.getDescendantOfName("blkAlias_25K").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);

                value = response.getAttribute("aliasPassword");
                this.mainComponent.getDescendantOfName("txtAliasPassword_25K").setValue(value, true);
                this.mainComponent.getDescendantOfName("blkAliasPassword_25K").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);

                this.setEditable(true);

            }

            details.prototype.initializeIdentityParam = function(event) {
				this.passedItems = {};
				this.passedItems["namePassed"] = tibco.admin.util.Validation.trimAll(this.mainComponent.getDescendantOfName("txtName_25").getValue())?true:false;
                var obj = this.mainComponent.getDescendantOfName("cmbURL_25K");
                this.passedItems["identityURL"] = tibco.admin.util.Validation.doValidateSelectBox(obj, tibco.admin.Main.getServer().getDynamicProperty("@@cerStoreURL_25K"));
				this.passedItems["identityAlias"] = tibco.admin.util.Validation.trimAll(this.mainComponent.getDescendantOfName("txtAlias_25K").getValue())?true:false;
				this.passedItems["identityAliasPwd"] = tibco.admin.util.Validation.trimAll(this.mainComponent.getDescendantOfName("txtAliasPassword_25K").getValue())?true:false;
				this.enableSaveBtn4Add();
            }

            details.prototype.setEditRequestDetails = function(objService) {
                this.setRequestDetails(objService);
            }

            details.prototype.setAddRequestDetails = function(objService) {
                this.setRequestDetails(objService);
            }

            details.prototype.setRequestDetails = function(objService) {
                objService.subtype = "PublicKeyIdentity";
                objService.userActionForKeyStoreSubType = this.mainComponent.getDescendantOfName("userActionForKeyStoreSubType_25K").getValue();
                if(objService.userActionForKeyStoreSubType == "KeyStoreConfiguration") {
                    objService.keystoreURL = this.mainComponent.getDescendantOfName("cmbURL_25K").getText();
                    objService.storeType = tibco.admin.Main.getServer().getDynamicProperty("@@JKS_25K");
                }
                else {
                    objService.keyStoreName = this.mainComponent.getDescendantOfName("cmbURL_25K").getText();
                    objService.keyStoreEntityID = this.mainComponent.getDescendantOfName("cmbURL_25K").getValue();
                }
                objService.alias = this.mainComponent.getDescendantOfName("txtAlias_25K").getValue();
                objService.aliasPassword = this.mainComponent.getDescendantOfName("txtAliasPassword_25K").getValue();
            }


            //call this method to begin the service call (eg.service.callgetKeystoresRegisteredWithEnterprise();)
            details.prototype.callgetKeystoresRegisteredWithEnterprise = function() {
                var objService = tibco.admin.Main.loadService("tibco.admin",
                        "tibco/admin/enterpriseAssets/shdResDefs/Identity/rules/getKeystore.xml",
                        "getKeystoresRegisteredWithEnterprise", "Get keystores")
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getKeystoreEndpointURL());
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.ongetKeystoresRegisteredWithEnterpriseSuccess);
                objService.doCall();
            };

            details.prototype.ongetKeystoresRegisteredWithEnterpriseSuccess = function(objEvent) {
                this.mainComponent.getDescendantOfName("cmbURL_25K").repaint();
            };

			/*070818
			* if user doesn't fishish all the required fields, the "save" button will be disabled.  part 2 for Identity
			*/
			details.prototype.valiIdentityURL = function(obj) {
				this.passedItems["identityURL"] = tibco.admin.util.Validation.doValidateSelectBox(obj, tibco.admin.Main.getServer().getDynamicProperty("@@cerStoreURL_25K"));
				this.enableSaveBtn4Add();
            }

			details.prototype.valiIdentityAlias = function(obj, strValue) {
                this.passedItems["identityAlias"] = tibco.admin.util.Validation.doValidateNotEmptyNoWhSpace(obj, strValue, tibco.admin.Main.getServer().getDynamicProperty("@@alias_25K"));
				this.enableSaveBtn4Add();
            }

            details.prototype.valiIdentityAliasPwd = function(obj, strValue) {
                this.passedItems["identityAliasPwd"] = tibco.admin.util.Validation.doValidateNotEmptyNoWhSpace(obj, strValue, tibco.admin.Main.getServer().getDynamicProperty("@@aliasPassword_25K"));
				this.enableSaveBtn4Add();
            }
        });

